class GiftsController < ApplicationController
  layout 'standard'
  
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    @gift_pages, @gifts = paginate :gifts, :per_page => 200
  end

  def show
    @gift = Gift.find(params[:id])
  end
  
  def image
    @image = Gift.find(params[:id])
    send_data @image.picture, :filename => "photo.jpg", :type => @image.content_type, :disposition => "inline"
  end
  
  def mark_purchased
    @gift = Gift.find(params[:id])
    @gift.purchase = '1'
    @gift.update_attributes(params[:gift])
    flash[:notice] = 'Gift was successfully marked as purchased.'
    redirect_to :action => 'list'
  end
  
  def undo_purchased
    @gift = Gift.find(params[:id])
    @gift.purchase = 'NULL'
    @gift.update_attributes(params[:gift])
    flash[:notice] = 'Gift was successfully returned to the list of gifts.'
    redirect_to :action => 'list'
  end

  def new
    @gift = Gift.new
  end

  def create
    @gift = Gift.new(params[:gift])
    if @gift.save
      flash[:notice] = 'Gift was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  def edit
    @gift = Gift.find(params[:id])
  end

  def update
    @gift = Gift.find(params[:id])
    if @gift.update_attributes(params[:gift])
      flash[:notice] = 'Gift was successfully updated.'
      redirect_to :action => 'show', :id => @gift
    else
      render :action => 'edit'
    end
  end

  def destroy
    Gift.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
end
